package com.vil.controller;

import com.vil.common.Constants;
import com.vil.common.ServerResponse;
import com.vil.entity.Admin;
import com.vil.entity.Park;
import com.vil.entity.ParkUse;
import com.vil.entity.User;
import com.vil.service.ParkService;
import com.vil.service.ParkUseService;
import com.vil.service.VillageService;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("parkingSpace")
public class ParkUsController  extends BaseController {

    @Autowired
    private ParkService parkService;
    @Autowired
    private ParkUseService parkuService;
    @Autowired
    private VillageService villageService;

    //去車位页面
    @RequestMapping(" parkingSpaceManagement.html")
    public String managersList(Model model, HttpSession session) {
        Admin currentAdmin = (Admin) session.getAttribute(Constants.USER_SESSION);
        if (currentAdmin==null){
            return "admin/login";
        }
        model.addAttribute("park", currentAdmin);
        ServerResponse serverResponse = parkService.getListByManyConditions(1, 5, null);
        model.addAttribute("park", serverResponse.getData());
//        model.addAttribute("url","/administrator/adminlist.html");
        return "admin/parkinglist";
    }
    @PostMapping("parkinglist.html")
    public ServerResponse showUserList(Model model, @RequestParam(value = "pageIndex",defaultValue = "1",required = false) Integer pageIndex,
                                       @RequestParam(value = "pageSize",defaultValue = "5",required = false) Integer pageSize,
                                       @RequestParam("condition") String  park_number,
                                       @RequestParam("condition1") String  park_name){

        Park park=new Park();
        park.setPark_number(park_number);
        park.setPark_name(park_name);
        return parkService.getListByManyConditions(pageIndex, pageSize, park);
    }
    //去新增頁面
    @RequestMapping("park_add.html")
    public String park_add(Model model) {
        ServerResponse serverResponse = villageService.getListVillage();
        model.addAttribute("vil",serverResponse.getData());
        return "admin/park_add";
    }

    //增加
    @PostMapping("/addpark")
    @ResponseBody
    public ServerResponse addParkSelective(Park park, HttpSession session) {

        return parkService.insertSelective(park);
    }


    //去编辑页面
    @GetMapping("editpark.html/{id}")
    public String toEditPark(@PathVariable String id, Model model){
        ServerResponse byId =parkService.getById(Long.valueOf(Integer.parseInt(id)));
        Park data =(Park) byId.getData();
        model.addAttribute("u_park",data);
        return "admin/parkedit";
    }


    //修改
    @PostMapping("editPark")
    @ResponseBody
    public ServerResponse editParkSelective(Park park){

        return parkService.updateByPrimaryKeySelective(park);
    }
    @DeleteMapping("deleteParkById/{id}")
    @ResponseBody
    public ServerResponse deleteParkById(@PathVariable Long id){

        return  parkService.deleteByPrimaryKey(id);
    }

    @PostMapping("deleteParkByIds")
    @ResponseBody
    public ServerResponse deleteParkByIds(String ids){

        String str[] = ids.split(",");
        Long array[] = (Long[]) ConvertUtils.convert(str,Long.class);
        return parkService.deleteMany(array);
    }
    //去車位使用页面
    @RequestMapping("parkingTrialManagement.html")
    public String managerParkUsesList(Model model, HttpSession session) {
        Admin currentAdmin = (Admin) session.getAttribute(Constants.USER_SESSION);
        if (currentAdmin==null){
            return "admin/login";
        }
        model.addAttribute("parkuse", currentAdmin);
        ServerResponse serverResponse = parkuService.getListByManyConditions(1, 5, null);
        model.addAttribute("parkuse", serverResponse.getData());
//        model.addAttribute("url","/administrator/adminlist.html");
        return "admin/parkingusagelist";
    }
    @PostMapping("parkingusagelist.html")
    public ServerResponse showParkUseList(Model model, @RequestParam(value = "pageIndex",defaultValue = "1",required = false) Integer pageIndex,
                                       @RequestParam(value = "pageSize",defaultValue = "5",required = false) Integer pageSize,
                                       @RequestParam("condition") String  owner,
                                       @RequestParam("condition1") String  car_number){

        ParkUse parku=new ParkUse();
        parku.setOwner(owner);
        parku.setCar_number(car_number);
        return parkuService.getListByManyConditions(pageIndex, pageSize, parku);
    }
    //去新增頁面
    @RequestMapping("parkingusageadd.html")
    public String parkingusageadd(Model model) {
        ServerResponse serverResponse = villageService.getListVillage();
        model.addAttribute("vil",serverResponse.getData());
        return "admin/parkingusageadd";
    }
    //增加
    @PostMapping("/parkingusageadd")
    @ResponseBody
    public ServerResponse addParkUseSelective(ParkUse parkUse, HttpSession session) {

        return parkuService.insertSelective(parkUse);
    }


    //去编辑页面
    @GetMapping("editparkUse.html/{id}")
    public String toEditParkUse(@PathVariable String id, Model model){
        ServerResponse byId =parkuService.getById(Long.valueOf(Integer.parseInt(id)));
        ParkUse data =(ParkUse) byId.getData();
        model.addAttribute("u_parku",data);
        ServerResponse serverResponse = villageService.getListVillage();
        model.addAttribute("vil",serverResponse.getData());
        return "admin/parkuedit";
    }


    //修改
    @PostMapping("editParkUse")
    @ResponseBody
    public ServerResponse editParkUseSelective(ParkUse parkUse){

        return parkuService.updateByPrimaryKeySelective(parkUse);
    }
    @DeleteMapping("deleteParkUseById/{id}")
    @ResponseBody
    public ServerResponse deleteParkUseById(@PathVariable Long id){

        return  parkuService.deleteByPrimaryKey(id);
    }

    @PostMapping("deleteParkUseByIds")
    @ResponseBody
    public ServerResponse deleteParkUseByIds(String ids){

        String str[] = ids.split(",");
        Long array[] = (Long[]) ConvertUtils.convert(str,Long.class);
        return parkuService.deleteMany(array);
    }

}
